home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6144 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  47 lines

  1. Path: savvy1.savvy.com!phil
  2. From: phil@savvy1.savvy.com (Phil Schwartz)
  3. Newsgroups: comp.lang.c
  4. Subject: function similar to strtok() needed
  5. Date: 22 Feb 1996 22:12:41 GMT
  6. Organization: Savvy Communications
  7. Message-ID: <4gipop$igp@savvy2.savvy.com>
  8. NNTP-Posting-Host: savvy1.savvy.com
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  
  12. I want to be able to parse a line, say "this is a test&it better work!ok"
  13. and grab the tokens which I will consider to be anything except the 
  14. space (" "), ampersand ("&") and exclamation point ("!").  Strtok() seems to 
  15. be a good starting point:
  16.  
  17. -----------------
  18. char *tokens = " !&";
  19. char *tok;
  20. char *string = "this is a test&it better work!ok";
  21.  
  22. tok = strtok(string, tokens);
  23. while (tok != NULL)  {
  24.     printf("%s\n", tok);
  25.     tok = strtok(NULL, tokens);
  26. }
  27.     ...
  28. -----------------
  29.  
  30.  
  31. However, what if I want to know which of the characters (in tokens) was 
  32. the separator between two tokens (tok)?  For example, when tok is "test" 
  33. the separator was a space and when tok is "it", the separator is the 
  34. ampersand.  
  35.  
  36. I'm sure I could add write a hocked up routine to do this, but I figure 
  37. there must be some elegant way of finding out this information.
  38.  
  39. Any help is appreciated.
  40.  
  41. Thanks,
  42.  
  43. Phil Schwartz
  44. phil@savvy.com
  45.  
  46.  
  47.